<?php

namespace app\rider\services;

use app\admin\model\OrderInModel;
use app\admin\model\ShopModel;
use think\facade\Validate;

class OrderService
{
    /**
     * 订单详情
     * @param $p
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function detail($p){
        $rule=[
            'id'=>'require',
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($p)){
            api_return(500,$validate->getError());
        }
        if(!$orderInfo=OrderInModel::find($p['id']))
        {
            api_return(500,'订单不存在');
        }
        api_return(200,'',$orderInfo);
    }

    /**
     * 拒绝订单
     * @param $p
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function refuse($p){
        $orderInfo=self::checkOrder($p);
        if($orderInfo->status!=1){
            api_return(500,'订单已接单，状态不可更改');
        }
        $orderInfo->status=3;
        $orderInfo->admin_id=$p['admin_id'];
        api_return(($orderInfo->save())?200:500);
    }

    /**
     * 订单列表
     * @param $p
     * @return void
     */
    public static function list($p){
       $params=[
           'where'=>[
               ['shop_id','=',$p['shop_id']],
               ['admin_id','=',$p['admin_id']],
           ],
           'page'=>($p['page'])??1,
           'pageSize'=>($p['pageSize'])??10,
       ];
       if(!empty($p['status']))
       {
           $params['where'][]=['status','=',$p['status']];
       }
       $list=OrderInModel::getList($params);
       $count=OrderInModel::count($params['where']);
       $groupList=OrderInModel::where([
           ['shop_id','=',$p['shop_id']],
           ['create_time','between',[date('Y-m-d').'00-00-00',date('Y-m-d').' 23:59:59']]
       ])->group('status')->column('count(*) as count','status');
       api_return(200,'',[
           'list'=>$list,
           'count'=>$count,
           'statistics'=>[
               'new'=>($groupList[1])??'0',
               'wait'=>($groupList[2])??'0',
               'total'=>OrderInModel::where([
                   ['shop_id','=',$p['shop_id']],
                   ['status','=',4],
                   ['create_time','between',[date('Y-m-d').'00-00-00',date('Y-m-d').' 23:59:59']]
               ])->sum('total_amount'),
               'shopInfo'=>ShopModel::find($p['shop_id'])
           ]
       ]);
    }

    /**
     * 接单
     * @param $p
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function accept($p){
        $orderInfo=self::checkOrder($p);
        if($orderInfo->status!=1){
            api_return(500,'订单状态不可更改');
        }
        $orderInfo->status=2;
        $orderInfo->admin_id=$p['admin_id'];
        $orderInfo->accept_time=date('Y-m-d H:i:s');
        api_return(($orderInfo->save())?200:500);
    }

    /**
     * 完成订单
     * @param $p
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function finish($p){
        $rule=[
            'id'=>'require',
            'realweight'=>'require',
            'total_amount'=>'require',
        ];
        self::checkOrder($p,$rule);
        $p['finish_time']=date('Y-m-d H:i:s');
        $p['status']=4;
        OrderInModel::edit($p);
    }

    /**
     * 核验订单合理性
     * @param $p
     * @param string[] $rule
     * @return OrderInModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected static function checkOrder($p,$rule=['id'=>'require']){
        $validate=Validate::rule($rule);
        if(!$validate->check($p)){
            api_return(500,$validate->getError());
        }
        if(!$orderInfo=OrderInModel::field('id,status')->find($p['id']))
        {
            api_return(500,'订单不存在');
        }
        return $orderInfo;
    }
}
